﻿/*
 * My Timer - frmTimersPopup.cs
 * 
 * Copyright (C) 2009 Ahmad Hanis Azmi
 * 
 * This file is part of My Timer
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace My_Timer
{
    public partial class frmTimersPopup : Form
    {
        private Point mouse_offset;

        public frmTimersPopup()
        {
            InitializeComponent();

            // seems that visual studio cannot save this in frmTimersPopup.Designer.cs
            FormClosed += new FormClosedEventHandler(frmTimersPopup_FormClosed);

            // move form when clicked on form or labelTimer: event capture
            MouseDown += new MouseEventHandler(frmTimersPopup_MouseDown);
            MouseMove += new MouseEventHandler(frmTimersPopup_MouseMove);
            labelTimer.MouseDown += new MouseEventHandler(labelTimer_MouseDown);
            labelTimer.MouseUp += new MouseEventHandler(labelTimer_MouseUp);
            labelTimer.MouseMove += new MouseEventHandler(labelTimer_MouseMove);
            labelTimer.MouseHover += new EventHandler(labelTimer_MouseHover);
            labelTimer.MouseLeave += new EventHandler(labelTimer_MouseLeave);

            // make pictureboxs look like button behaviour when clicked
            pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            pictureBox2.MouseDown += new MouseEventHandler(pictureBox2_MouseDown);
            pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
            pictureBox2.MouseUp += new MouseEventHandler(pictureBox2_MouseUp);
            pictureBox1.MouseHover += new EventHandler(pictureBox1_MouseHover);
            pictureBox1.MouseLeave += new EventHandler(pictureBox1_MouseLeave);
            pictureBox2.MouseHover += new EventHandler(pictureBox2_MouseHover);
            pictureBox2.MouseLeave += new EventHandler(pictureBox2_MouseLeave);

            MouseUp += new MouseEventHandler(frmTimersPopup_MouseUp);

            MouseHover += new EventHandler(frmTimersPopup_MouseHover);
            MouseLeave += new EventHandler(frmTimersPopup_MouseLeave);
        }

        private void frmTimersPopup_Load(object sender, EventArgs e)
        {
            int i = (int)Tag;

            if (GlobalClass.r_TimerControl.ContainsKey(i) == true)
            {
                if (GlobalClass.r_TimerControl[i].Enabled == true)
                {
                    pictureBox1.Image = global::My_Timer.Properties.Resources.media_playback_pause_16x16;
                }
            }
        }

        private void frmTimersPopup_MouseUp(object sender, MouseEventArgs e)
        {
            //MessageBox.Show(e.ToString());
            show_RightClickMenu(e);
        }

        private void labelTimer_MouseUp(object sender, MouseEventArgs e)
        {
            show_RightClickMenu(e);
        }

        private void frmTimersPopup_MouseHover(object sender, EventArgs e)
        {
            f_change_Opacity_MouseHover();
        }

        private void frmTimersPopup_MouseLeave(object sender, EventArgs e)
        {
            f_change_Opacity_MouseLeave();
        }

        private void labelTimer_MouseHover(object sender, EventArgs e)
        {
            f_change_Opacity_MouseHover();
        }

        private void labelTimer_MouseLeave(object sender, EventArgs e)
        {
            f_change_Opacity_MouseLeave();
        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            f_change_Opacity_MouseHover();
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            f_change_Opacity_MouseLeave();
        }

        private void pictureBox2_MouseHover(object sender, EventArgs e)
        {
            f_change_Opacity_MouseHover();
        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            f_change_Opacity_MouseLeave();
        }

        private void f_change_Opacity_MouseHover()
        {
            try
            {
                timerOpacityControl.Enabled = false;
                int i = (int)Tag;

                TimersArray t_TimersArray;
                t_TimersArray = GlobalClass.r_Timers[i];

                double d = t_TimersArray.form_Opacity;
                double d2 = 1 - d;
                d2 = (d2 / 2);
                d = d + d2;
                Opacity = d;
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show("Error caught in f_change_Opacity_MouseHover. Error: " + ex.Message);
#endif
            }
        }

        private void f_change_Opacity_MouseLeave()
        {
            timerOpacityControl.Enabled = true;
        }

        private void show_RightClickMenu(MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    Point mouse_Location = Control.MousePosition;
                    //MessageBox.Show(mouse_Location.ToString());
                    contextMenuStrip1.Show(mouse_Location);

                    int i = (int)Tag;

                    TimersArray t_TimersArray;
                    t_TimersArray = GlobalClass.r_Timers[i];

                    if (t_TimersArray.form_AlwaysOnTop == true) 
                    { alwaysOnTopToolStripMenuItem.Checked = true; }
                    else { alwaysOnTopToolStripMenuItem.Checked = false; }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show("Error caught in show_RightClickMenu. Error: " + ex.Message);
#endif
            }
        }

        private void frmTimersPopup_FormClosed(object sender, EventArgs e)
        {
            int i = (int)Tag;

            TimersArray t_TimersArray;
            t_TimersArray = GlobalClass.r_Timers[i];

            int iIndexOfKey = t_TimersArray.iIndexOfKey;

            //GlobalClass.r_frmMain.lvTimers.Items[i].Checked = false;
            if ((GlobalClass.r_frmMain.Created == true) || (GlobalClass.r_frmMain.Visible == true))
            {
                GlobalClass.r_frmMain.lvTimers.Items[iIndexOfKey].Font = new Font("Microsoft Sans Serif",
                    (float)8.25, FontStyle.Regular);
            }

            t_TimersArray.IsFormEnable = false;

            GlobalClass.r_Timers[i] = t_TimersArray;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y + 1);
            }
        }

        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                pictureBox2.Location = new Point(pictureBox2.Location.X + 1, pictureBox2.Location.Y + 1);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X - 1, pictureBox1.Location.Y - 1);

                int i_Tag = (int)Tag;

                if (GlobalClass.r_TimerControl.ContainsKey(i_Tag) == true)
                {
                    switch (GlobalClass.r_TimerControl[i_Tag].Enabled)
                    {
                        case false:
                            TimerControl.StartTimer(i_Tag);

                            break;

                        case true:
                            TimerControl.PauseTimer(i_Tag);

                            break;
                    }
                }
                else
                {
                    TimerControl.StartTimer(i_Tag);
                }
            }
        }

        private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            frmTray.doPlayAudio(1, null);

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                pictureBox2.Location = new Point(pictureBox2.Location.X - 1, pictureBox2.Location.Y - 1);

                int i_Tag = (int)Tag;

                TimerControl.StopTimer(i_Tag);
            }
        }

        // move form when clicked on form or labelTimer
        private void frmTimersPopup_MouseDown(object sender, MouseEventArgs e)
        {
            set_Mouse_Offset(e);
        }

        // move form when clicked on form or labelTimer
        private void labelTimer_MouseDown(object sender, MouseEventArgs e)
        {
            set_Mouse_Offset(e);
        }

        private void set_Mouse_Offset(MouseEventArgs e)
        {
            mouse_offset = new Point(-e.X, -e.Y);
        }

        // move form when clicked on form or labelTimer
        private void frmTimersPopup_MouseMove(object sender, MouseEventArgs e)
        {
            move_Form(e);
        }

        // move form when clicked on form or labelTimer
        private void labelTimer_MouseMove(object sender, MouseEventArgs e)
        {
            move_Form(e);
        }

        private void move_Form(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Point mouse_pos = Control.MousePosition;
                mouse_pos.Offset(mouse_offset.X, mouse_offset.Y);

                Location = mouse_pos;
            }
        }

        private void frmTimersPopup_Move(object sender, EventArgs e)
        {
            try
            {
                int i = (int)Tag;

                TimersArray t_TimersArray;
                t_TimersArray = GlobalClass.r_Timers[i];

                t_TimersArray.xPossition = Location.X;
                t_TimersArray.yPossition = Location.Y;

                GlobalClass.r_Timers[i] = t_TimersArray;
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show("Error caught in frmTimersPopup_Move. Error: " + ex.Message);
#endif
            }
        }

        private void frmTimersPopup_Resize(object sender, EventArgs e)
        {
            int i = (int)Tag;

            TimersArray t_TimersArray;
            t_TimersArray = GlobalClass.r_Timers[i];

            //double rWidth = 6.5;
            double rWidth = 6.5;
            int fWidth = Width;
            int fHeight = Height;

            double fontSize = Height - 75;
            double fontWidth = fontSize / rWidth;

            // todo: postponed: max and minimum height

            if ((Width - 5) > fontWidth)
            {
                fontSize = ((Width - 5) / rWidth);
            }

            if (fontSize < 1)
            {
                fontSize = 1;
            }

            labelTimer.Font = new System.Drawing.Font("Arial", (float)fontSize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
            t_TimersArray.xSize = Width;
            t_TimersArray.ySize = Height;

            GlobalClass.r_Timers[i] = t_TimersArray;

            //MessageBox.Show(this.WindowState.ToString());

            if (WindowState == FormWindowState.Minimized)
            {
                Show();
                SendToBack();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            // see pictureBox1_MouseUp event
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            // see pictureBox2_MouseUp event
        }

        private void transparencyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Point mouse_Position = Control.MousePosition;
            //MessageBox.Show(Screen.PrimaryScreen.Bounds.X.ToString());

            frmChangeOpacity r_frmChangeTransparency = new frmChangeOpacity();
            //r_frmChangeTransparency.Parent = this;
            r_frmChangeTransparency.Tag = Tag;
            r_frmChangeTransparency.Show(this);
            if ((mouse_Position.X + 260) > Screen.PrimaryScreen.Bounds.Width)
            {
                mouse_Position = new Point(Control.MousePosition.X - 260, Control.MousePosition.Y);
            }
            r_frmChangeTransparency.Location = mouse_Position;
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }

        private void labelTimerArray_Click(object sender, EventArgs e)
        {

        }

        private void timerOpacityControl_Tick(object sender, EventArgs e)
        {
            try
            {
                int i = (int)Tag;

                TimersArray t_TimersArray;
                t_TimersArray = GlobalClass.r_Timers[i];

                Opacity = t_TimersArray.form_Opacity;
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show("Error caught in timerOpacityControl_Tick. Error: " + ex.Message);
#endif
            }
        }

        private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int i = (int)Tag;

            TimersArray t_TimersArray;
            t_TimersArray = GlobalClass.r_Timers[i];

            switch (t_TimersArray.form_AlwaysOnTop)
            {
                case true:
                    TopMost = false;
                    t_TimersArray.form_AlwaysOnTop = false;

                    break;

                case false:
                    TopMost = true;
                    t_TimersArray.form_AlwaysOnTop = true;

                    break;
            }

            GlobalClass.r_Timers[i] = t_TimersArray;
        }
    }
}
